package com.jhgsys.internal.accident.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhgsys.internal.accident.entity.HazardExamineFile;
import com.jhgsys.internal.accident.mapper.HazardExamineFileMapper;
import com.jhgsys.internal.accident.service.IHazardExamineFileService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  Service实现
 *
 * @author zhangwei
 * @date 2024-08-22 13:43:29
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HazardExamineFileServiceImpl extends ServiceImpl<HazardExamineFileMapper, HazardExamineFile> implements IHazardExamineFileService {

    /**
     * 查询（分页）
     * @author zhangwei
     * @date 2024-08-22 13:43:29
     * @param request QueryRequest
     * @param hazardExamineFile hazardExamineFile
     * @return IPage<HazardExamineFile>
    */
    @Override
    public IPage<HazardExamineFile> findHazardExamineFilePage(QueryRequest request, HazardExamineFile hazardExamineFile) {
        LambdaQueryWrapper<HazardExamineFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardExamineFile::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<HazardExamineFile> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author zhangwei
     * @date 2024-08-22 13:43:29
     * @param hazardExamineFile hazardExamineFile
     * @return List<HazardExamineFile>
     */
    @Override
    public List<HazardExamineFile> findHazardExamineFileList(HazardExamineFile hazardExamineFile) {
	    LambdaQueryWrapper<HazardExamineFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardExamineFile::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author zhangwei
    * @date 2024-08-22 13:43:29
    * @param id
    * @return HazardExamineFile
    */
    @Override
    public HazardExamineFile findHazardExamineFileById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author zhangwei
     * @date 2024-08-22 13:43:29
     * @param hazardExamineFile hazardExamineFile
     */
    @Override
    @Transactional
    public void createHazardExamineFile(HazardExamineFile hazardExamineFile) {
        hazardExamineFile.setCreatedTime(new Date());
        this.save(hazardExamineFile);
    }

    /**
     * 修改
     * @author zhangwei
     * @date 2024-08-22 13:43:29
     * @param hazardExamineFile hazardExamineFile
     */
    @Override
    @Transactional
    public void updateHazardExamineFile(HazardExamineFile hazardExamineFile) {
        hazardExamineFile.setUpdatedTime(new Date());
        this.saveOrUpdate(hazardExamineFile);
    }

    /**
     * 逻辑删除
     * @author zhangwei
     * @date 2024-08-22 13:43:29
     * @param hazardExamineFile hazardExamineFile
     * @param hazardExamineFileIds hazardExamineFileIds
     */
    @Override
    @Transactional
    public void deleteHazardExamineFile(HazardExamineFile hazardExamineFile,String hazardExamineFileIds) {
        List<String> list = Arrays.asList(hazardExamineFileIds.split(StringPool.COMMA));
        this.baseMapper.update(hazardExamineFile,new LambdaQueryWrapper<HazardExamineFile>().in(HazardExamineFile::getExamineFileId,list));
	}

    /**
    * 物理删除
    * @author zhangwei
    * @date 2024-08-22 13:43:29
    * @param hazardExamineFileIds hazardExamineFileIds
    */
    @Override
    @Transactional
    public void physicallyDeleteHazardExamineFile(String hazardExamineFileIds) {
        List<String> list = Arrays.asList(hazardExamineFileIds.split(StringPool.COMMA));
        LambdaQueryWrapper<HazardExamineFile> wapper = new LambdaQueryWrapper<>();
        wapper.in(HazardExamineFile::getExamineFileId,list);
        this.remove(wapper);
    }

    /**
     * 根据安全检查记录id删除
     * @author admin
     * @date 2024-08-22 09:56:15
     * @param reportIds
     * @param fileType 文件类型;1：问题文件 2：整改文件
     */
    @Override
    @Transactional
    public void deleteByExamineRecordIds(String projectIds,String fileType) {
        List<String> list = Arrays.asList(projectIds.split(StringPool.COMMA));
        LambdaQueryWrapper<HazardExamineFile> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(HazardExamineFile::getExamineRecordId,list);
        wrapper.eq(HazardExamineFile::getExamineFileType,fileType);
        this.remove(wrapper);
    }
}
